//
//  WithdrawTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2024/3/8.
//

import UIKit

class WithdrawTableViewCell: BaseTableViewCell {

    private let itemHeight: CGFloat = 76.0
    
    private var currentSelect = 0
    var selectHandle: ((Int)->Void)?
    private(set) var pricesArr = [WithdrawPriceModel]()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    
    static func getCellHeight() -> CGFloat {
        return 42 + 14 + 2 + 37 + 66 + 62 + 22 + 76
    }
    
    func setInfo(balance: String, pricesArr: [WithdrawPriceModel]) {
        self.pricesArr = pricesArr
        priceLabel.text = balance
        priceCollectionView.reloadData()
    }
    
    func updatePlateformBindStatus(_ status: String) {
        plaformView.updateDetail(status)
    }
    
    // MARK: - UIButtonAction
    @objc private func platformViewGRAction() {
        send(event: WithdrawVC.TransferEvent.clickPlatform)
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(earninsBackView)
        earninsBackView.addSubview(earninsTitleLabel)
        earninsBackView.addSubview(priceLabel)
        contentView.addSubview(plaformView)
        contentView.addSubview(priceCollectionView)
        
        earninsBackView.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(42)
        }
        earninsTitleLabel.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(14)
        }
        priceLabel.snp.makeConstraints { make in
            make.top.equalTo(earninsTitleLabel.snp.bottom).offset(2)
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(37)
        }
        plaformView.snp.makeConstraints { make in
            make.left.right.equalTo(earninsBackView)
            make.top.equalTo(earninsBackView.snp.bottom).offset(66)
            make.height.equalTo(62)
        }
        priceCollectionView.snp.makeConstraints { make in
            make.left.right.equalTo(plaformView)
            make.top.equalTo(plaformView.snp.bottom).offset(22)
            make.height.equalTo(itemHeight)
        }
        
        let platformViewGR = UITapGestureRecognizer(target: self, action: #selector(platformViewGRAction))
        plaformView.addGestureRecognizer(platformViewGR)
        
        priceCollectionView.delegate = self
        priceCollectionView.dataSource = self
        priceCollectionView.register(cellClass: WithdrawPriceCollectionViewCell.self)
        
    }
    private lazy var earninsBackView = UIView()
    private lazy var earninsTitleLabel: UILabel = {
        let earninsTitleLabel = UILabel()
        earninsTitleLabel.text = "总金额(元)".localized
        earninsTitleLabel.font = kSetPingFangRegular(12)
        earninsTitleLabel.textColor = .app_textGrayColor
        earninsTitleLabel.textAlignment = .center
        return earninsTitleLabel
    }()
    private lazy var priceLabel: UILabel = {
        let priceLabel = UILabel()
        priceLabel.textColor = .app_priceTextColor
        priceLabel.font = kSetPingFangBlod(35)
        priceLabel.textAlignment = .center
        return priceLabel
    }()
    private lazy var plaformView = WithdrawPlatformView(icon: "invite_wechat", name: "微信提现".localized, detail: "未绑定账号".localized)
    private lazy var priceCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        let priceCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        priceCollectionView.backgroundColor = .clear
        priceCollectionView.showsHorizontalScrollIndicator = false
        return priceCollectionView
    }()
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension WithdrawTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 8
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 8
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (collectionView.width-8*2)/3, height: itemHeight)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pricesArr.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(WithdrawPriceCollectionViewCell.self, indexPath: indexPath)
        cell.setInfo(priceModel: pricesArr[indexPath.item], isSelect: indexPath.row == currentSelect)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        currentSelect = indexPath.item
        collectionView.reloadData()
        
        guard let handle = selectHandle else { return }
        handle(currentSelect)
    }
    
}


